
#include "NetMsgHandler.h"
#include "NetMapHandler.h"

#include "CreatureManager.h"

#include "MapManager.h"
#include "GameDanger.h"




fint32		NetMoveHandler( GamePlayer* player , netMsgHead* head )
{
    SEND_RECV_MSG_MOVE* msg = (SEND_RECV_MSG_MOVE*)head;

    if ( player->GetTeam() )
	{
		if ( !player->GetTeam()->IsLeader( player ) )
		{
			player->AddDanger( GDLEVEL10 );
			return GAME_ERROR_TEAMMOVE;
		}
	}

	if ( msg->MapID != player->GetMoveData().Pos.MapID )
	{
		return GAME_NO_ERROR;
	}

	MapScene* scene = gMapManager->GetScene( player->GetMoveData().Pos.MapID );
	fbool b = scene->AddMove( player , msg->Path , msg->Count );

	if ( b )
	{
		msg->type = _MSG_RECV_MOVE;
		msg->GUID = player->GetGUID();
		scene->SendMsgToAllMove( msg , player->GetGUID() );
	}

	return GAME_NO_ERROR;
}

fint32		NetJoinTeamHandler( GamePlayer* player , netMsgHead* head )
{
    CHECKMSG( SEND_MSG_JOIN_TEAM )
    
	GamePlayer* leader = gCreatureManager->GetPlayer( msg->GUID );

	if ( !leader )
	{
		return GAME_ERROR_PLAYER_GUIDERROR;
	}

	MapPos pos1 = player->GetMoveData().Pos;
	MapPos pos2 = leader->GetMoveData().Pos;

	if ( pos1.MapID != pos2.MapID || pos1.Grid.distance( pos2.Grid ) > 2 )
	{
		return GAME_NO_ERROR;
	}

	gMapManager->JoinTeam( leader , player );
	
	return GAME_NO_ERROR;
}

fint32		NetLevelTeamHandler( GamePlayer* player , netMsgHead* head )
{
    CHECKMSG( SEND_MSG_LEAVE_TEAM )
    
	gMapManager->LeaveTeam( player );
	
	return GAME_NO_ERROR;
}
